package com.asia5b.a5mall.order.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.databinding.ViewDataBinding
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.order.AftersaleModel
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.bean.OrderListType
import com.asia5b.a5mall.middleware.model.order.ParcelModel
import com.asia5b.a5mall.middleware.model.bean.MenuBean
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.databinding.ItemOrderlistAdapterBinding
import com.asia5b.a5mall.order.databinding.ItemOrderlistDiffAdapterBinding
import com.asia5b.a5mall.order.databinding.ItemOrderlistParcelAdapterBinding
import com.asia5b.a5mall.order.widget.OrderGoodsView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/1/14.
 */
class OrderListAdapter<T : ViewDataBinding>(layoutResId: Int, list: ArrayList<OrderDetailModel>, var type: OrderListType) :
        BaseQuickAdapter<OrderDetailModel, BaseDataBindingHolder<T>>(layoutResId, list) {

    init {
        addChildClickViewIds(R.id.view_orderlist_goods_click)
        addChildClickViewIds(R.id.iv_orderlist_check)
    }

    companion object {
        fun newAdapter(type: OrderListType, mDatas: ArrayList<OrderDetailModel>): OrderListAdapter<*> {
            return when (type) {
                OrderListType.APPLY_PACKAGING -> {
                    OrderListAdapter<ItemOrderlistParcelAdapterBinding>(R.layout.item_orderlist_parcel_adapter, mDatas, type)
                }
                OrderListType.PAY_DIFF -> {
                    OrderListAdapter<ItemOrderlistDiffAdapterBinding>(R.layout.item_orderlist_diff_adapter, mDatas, type)
                }
                else -> {
                    OrderListAdapter<ItemOrderlistAdapterBinding>(R.layout.item_orderlist_adapter, mDatas, type)
                }
            }
        }
    }

    var isShowDelete: Boolean = false
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun convert(helper: BaseDataBindingHolder<T>, item: OrderDetailModel) {

        when (type) {
            OrderListType.APPLY_PACKAGING -> {
                setParcelView(helper, item)
            }
            OrderListType.PAY_DIFF -> {
                setDiffView(helper, item)
            }
            else -> {
                setAllView(helper, item)
            }
        }


    }

    /**
     * 所有订单的布局
     */
    fun setAllView(helper: BaseDataBindingHolder<T>, item: OrderDetailModel) {

        val binding = helper.dataBinding!! as ItemOrderlistAdapterBinding
        binding.model = item

        val ll_orde_goodslist = helper.getView<OrderGoodsView>(R.id.ll_orde_goodslist)
        ll_orde_goodslist.setGoodsList(item, type, item.order_sku_list)

        //点击事件
        setActionsView(helper, item)


    }

    /**
     * 补差价
     */
    fun setDiffView(helper: BaseDataBindingHolder<T>, item: OrderDetailModel) {
        val binding = helper.dataBinding!! as ItemOrderlistDiffAdapterBinding
        binding.model = item

        val ll_orde_goodslist = helper.getView<OrderGoodsView>(R.id.ll_orde_goodslist)
        val goodsList = arrayListOf<AbsGoodsModel>()
        item?.sent_parcel?.forEach { parcelModel ->
            parcelModel?.goods_list?.let {
                goodsList.addAll(it)
            }
        }
        ll_orde_goodslist.setGoodsList(item, type, goodsList)


        //todo 补差价
        helper.setGone(R.id.ll_orderlist_diffprice, true)

        //点击事件
        setActionsView(helper, item)
    }

    /**
     * 申请打包
     */
    fun setParcelView(helper: BaseDataBindingHolder<T>, item: OrderDetailModel) {
        val binding = helper.dataBinding!! as ItemOrderlistParcelAdapterBinding
        binding.model = item

        //复选框
        val iv_orderlist_check = helper.getView<ImageView>(R.id.iv_orderlist_check)
        iv_orderlist_check.visibility = View.VISIBLE
        if (item.isCanParcel) {

            if (item.notifyChecked()) {
                iv_orderlist_check.setImageResource(R.drawable.ic_check_press)
            } else {
                iv_orderlist_check.setImageResource(R.drawable.ic_check_normal)
            }
//            iv_orderlist_check.isSelected = item.notifyChecked()
        } else {
            iv_orderlist_check.setImageResource(R.drawable.ic_orderlist_uncheck)
        }


        val parcelRecyclerView = helper.getView<RecyclerView>(R.id.rv_orderlist_parcel)
        if (!item.sent_parcel.isNullOrEmpty()) {
            val iterator = item.sent_parcel!!.iterator()
            while (iterator.hasNext()) {
                if (iterator.next().goods_list.isNullOrEmpty()) {
                    iterator.remove()
                }
            }
            val orderListParcelAdapter = OrderListParcelAdapter(item, item.sent_parcel!!)
            orderListParcelAdapter.setOnItemChildClickListener { adapter, view, position ->
                val parcelModel = adapter.getItem(position) as ParcelModel
                //反选复选框
                view.isSelected = !view.isSelected
                parcelModel.checked = !parcelModel.checked
//                iv_orderlist_check.isSelected = item.notifyChecked()
                if (item.notifyChecked()) {
                    iv_orderlist_check.setImageResource(R.drawable.ic_check_press)
                } else {
                    iv_orderlist_check.setImageResource(R.drawable.ic_check_normal)
                }
                getOnItemChildClickListener()!!.onItemChildClick(adapter, parcelRecyclerView, helper.layoutPosition)
            }
            parcelRecyclerView.run {
                layoutManager = LinearLayoutManager(context)!!
                adapter = orderListParcelAdapter
            }
        }

    }

    fun hasApplyServiceAction(actionList: ArrayList<MenuBean>): Boolean {
        for (menuBean in actionList) {
            if (TextUtils.equals(menuBean.value, "allow_apply_service")) {
                return true
            }
        }
        return false
    }

    /**
     * 设置一系列点击事件
     */
    fun setActionsView(helper: BaseViewHolder, item: OrderDetailModel) {

        val actionRecyclerView = helper.getView<RecyclerView>(R.id.rv_orderlist_action)
        var actionList = arrayListOf<MenuBean>()
        //防止数据被改变
        actionList.addAll(item.getActionList())
        if (type == OrderListType.WAIT_REFUND && item is AftersaleModel) {
            actionList = arrayListOf<MenuBean>()
            actionList.add(0, MenuBean(0, "watch_refund_detail", ResString.get(R.string.查看退款)))//黑色
            System.out.println("---<<<>>>---refund_status:${item.refund_status},sn:${item.sn}");
            if (TextUtils.equals(item.refund_status, "APPLY")) {
                actionList.add(MenuBean(1, "cancel_refund_detail", ResString.get(R.string.取消退款)))//蓝色
            }
        } else if (type == OrderListType.WAIT_COMMENT) {//带评论可能存在申请售后的按钮
            if (!hasApplyServiceAction(actionList)) {
                item.order_sku_list?.run {
                    for (absGoodsModel in this) {
                        if (absGoodsModel.absGoodsAction?.allow_apply_service ?: false) {
                            actionList.add(0, MenuBean(0, "allow_apply_service", ResString.get(R.string.申请售后2)))
                            break
                        }
                    }
                }
            }
        }

        if (type == OrderListType.PAY_DIFF) {
            for (menuBean in actionList) {
                if (TextUtils.equals(menuBean.name, "allow_pay")) {
                    menuBean.value = ResString.get(R.string.补差价)
                    break
                }
            }
        }

        if (actionList.size > 0) {
            val mAdapter = OrderDetailmAdapter(actionList)
            actionRecyclerView.run {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)!!
                adapter = mAdapter
            }
            mAdapter.setOnItemClickListener { adapter, view, position ->
                view.tag = item
                getOnItemChildClickListener()!!.onItemChildClick(adapter,view,position)
            }
        }
        helper.setGone(R.id.rv_orderlist_action, actionList.size == 0)
    }

}